package com.farm.portal.dao.impl;

import java.math.BigInteger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.farm.portal.domain.Componentexcute;
import com.farm.portal.dao.ComponentexcuteDaoInter;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.utils.HibernateSQLTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/* *
 *功能：用户组件实例持久层实现
 *详细：
 *
 *版本：v0.1
 *作者：FarmCode代码工程
 *日期：20141204174206
 *说明：
 */
public class ComponentexcuteDaoImpl implements ComponentexcuteDaoInter {
	private SessionFactory sessionFatory;
	private HibernateSQLTools<Componentexcute> sqlTools;

	@Override
	public void deleteEntity(Componentexcute componentexcute) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		session.delete(componentexcute);
	}

	@Override
	public int getAllListNum() {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		SQLQuery sqlquery = session
				.createSQLQuery("select count(*) from farm_code_field");
		BigInteger num = (BigInteger) sqlquery.list().get(0);
		return num.intValue();
	}

	@Override
	public Componentexcute getEntity(String componentexcuteid) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		return (Componentexcute) session.get(Componentexcute.class,
				componentexcuteid);
	}

	@Override
	public Componentexcute insertEntity(Componentexcute componentexcute) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		session.save(componentexcute);
		return componentexcute;
	}

	@Override
	public void editEntity(Componentexcute componentexcute) {
		// TODO 自动生成代码,修改后请去除本注释
		Session session = sessionFatory.getCurrentSession();
		session.update(componentexcute);
	}

	@Override
	public Session getSession() {
		// TODO 自动生成代码,修改后请去除本注释
		return sessionFatory.getCurrentSession();
	}

	@Override
	public DataResult runSqlQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			return query.search(sessionFatory.getCurrentSession());
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void deleteEntitys(List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		sqlTools
				.deleteSqlFromFunction(sessionFatory.getCurrentSession(), rules);
	}

	@Override
	public List<Componentexcute> selectEntitys(List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		return sqlTools.selectSqlFromFunction(
				sessionFatory.getCurrentSession(), rules);
	}

	@Override
	public void updataEntitys(Map<String, Object> values, List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		sqlTools.updataSqlFromFunction(sessionFatory.getCurrentSession(),
				values, rules);
	}

	@Override
	public int countEntitys(List<DBRule> rules) {
		// TODO 自动生成代码,修改后请去除本注释
		return sqlTools.countSqlFromFunction(sessionFatory.getCurrentSession(),
				rules);
	}

	public SessionFactory getSessionFatory() {
		return sessionFatory;
	}

	public void setSessionFatory(SessionFactory sessionFatory) {
		this.sessionFatory = sessionFatory;
	}

	public HibernateSQLTools<Componentexcute> getSqlTools() {
		return sqlTools;
	}

	public void setSqlTools(HibernateSQLTools<Componentexcute> sqlTools) {
		this.sqlTools = sqlTools;
	}

	@Override
	public Componentexcute getPortalExcuteOrInit(String portalId,
			String componentDefId, String portalExcuteId, String userId,
			String userName) {
		List<Componentexcute> list = this.selectEntitys(DBRule.addRule(DBRule
				.addRule(new ArrayList<DBRule>(), "COMPONENTDEFID",
						componentDefId, "="), "PORTALEXCUTEID", portalExcuteId,
				"="));
		if (list.size() > 0) {
			return list.get(0);
		} else {
			Componentexcute excute = new Componentexcute();
			excute.setCloseis("0");
			excute.setCollapsis("0");
			excute.setComponentdefid(componentDefId);
			excute.setPortaldefid(portalId);
			excute.setPortalexcuteid(portalExcuteId);
			excute.setSort(0);
			this.insertEntity(excute);
			return excute;
		}
	}
}
